<?php
//=======================================
//根据时间等作为种子，产生非重复的数据库ID序列
//Time:2008-10-27
//Author:王照暄
//=======================================
function base64url_encode($plainText) {
    $base64 = base64_encode($plainText);
    $base64url = strtr($base64, '+/=', '-__');
    return ($base64url);   
}

function get_unique_id() {
    return base64url_encode(pack('H*', uniqid(rand()%256))) . dechex(rand()%256);
}

function is_id_valid($id) {
    $id = "$id";
    if ($id === "" || $id === "0" || $id === "NaN" || $id === "undefined" || $id === "deleted") {
        return false;
    }
    return strspn($id, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_') == strlen($id);
}

function is_session_id_valid($id) {
    return strspn($id, '0123456789abcdef') == 32 && strlen($id) == 32;
}


function get_user_picture_path($id) {
    if (!is_id_valid($id))
        return false;    
    if(is_numeric($id)) {
        $userpath = "0/0/$id";
    } else {
        $dir0 = substr($id, 0, 2);
        $dir1 = substr($id, 2, 2);
        $userpath = "$dir0/$dir1/$id";
    }
    return $userpath;
}
?>
